package com.lrabbit.rabbit.user.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrabbit.controller.BaseController;
import com.lrabbit.msg.CommResponse;
import com.lrabbit.rabbit.api.user.SysUserApi;
import com.lrabbit.rabbit.api.user.request.SysUserAddReq;
import com.lrabbit.rabbit.api.user.request.SysUserPageReq;
import com.lrabbit.rabbit.api.user.request.SysUserSearchReq;
import com.lrabbit.rabbit.api.user.request.SysUserUpdateReq;
import com.lrabbit.rabbit.api.user.response.SysUserVO;
import com.lrabbit.rabbit.user.entity.SysUserDO;
import com.lrabbit.rabbit.user.service.SysUserService;
import com.lrabbit.utils.StrUtils;
import com.lrabbit.utils.UUIDUtil;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户表 自定义Controller
 *
 * @author common-generator
 * @since 2024-06-22
 */
@Slf4j
@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*", maxAge = 3600)
public class SysUserController extends BaseController implements SysUserApi {

    // 如果逻辑复杂，方法需要在service单独定义!
    @Resource
    SysUserService sysUserService;

    @Override
    public CommResponse addSysUser(@Valid SysUserAddReq addReq) {
//      UserInfo userInfo = BaseContextHandler.getUserInfo();
        log.info("addResource req={}", JSONUtil.toJsonStr(addReq));
        SysUserDO entity = new SysUserDO();
        BeanUtils.copyProperties(addReq, entity);
        entity.setId(UUIDUtil.getSnowflakeStrId());
        // 更新时间，创建时间，新增人，用户权限
        log.info("addResource entity={}", JSONUtil.toJsonStr(entity));
        boolean flagSave = sysUserService.save(entity);
        if (flagSave) {
            return back("success");
        }
        return backError();
    }

    @Override
    public CommResponse updateSysUser(@Valid SysUserUpdateReq updateReq) {
        log.info("updateResource req={}", JSONUtil.toJsonStr(updateReq));
        SysUserDO entity = new SysUserDO();
        BeanUtils.copyProperties(updateReq, entity);
        boolean flagSave = sysUserService.updateById(entity);
        if (flagSave) {
            return back("success");
        }
        return backError();
    }

    @Override
    public CommResponse deleteSysUser(String id) {
        log.info("deleteResource id={}", id);
        boolean flagSave = sysUserService.removeById(id);
        if (flagSave) {
            return back("success");
        }
        return backError();
    }

    @Override
    public CommResponse<List<SysUserVO>> listSysUser(SysUserSearchReq searchReq) {
        QueryWrapper<SysUserDO> queryWrapper = new QueryWrapper<>();
        List<SysUserDO> list = sysUserService.list(queryWrapper);
        return back(list, SysUserVO.class);
    }

    @Override
    public CommResponse<SysUserVO> detailSysUser(String id) {
        SysUserDO entity = sysUserService.getById(id);
        if (!StrUtils.isBlank(entity)) {
            return back(entity, SysUserVO.class);
        }
        return backError("未查到数据");
    }

    @Override
    public CommResponse<List<SysUserVO>> listPageSysUser(@Valid SysUserPageReq pageReq) {
        // 分页查询
        Page pageList = sysUserService.listPageSysUserList(pageReq);
        log.info("分页查询结束-查询条数{},总记录数{}", pageList.getRecords().size(), pageList.getTotal());
        return back(pageList, SysUserVO.class);
    }

}
